import 'package:get/get.dart';
import 'package:mziot/app/config/constant.dart';
import 'package:mziot/app/modules/auth/login/insweb/views/insweb_view.dart';
import 'package:mziot/app/modules/auth/login/views/auth_login_view.dart';
import 'package:mziot/app/modules/auth/password/views/password_view.dart';
import 'package:mziot/app/modules/auth/profile/views/auth_profile_view.dart';
import 'package:mziot/app/modules/auth/register/views/auth_register_view.dart';
import 'package:mziot/app/modules/circle/views/circle_detail_view.dart';
import 'package:mziot/app/modules/circle/views/circle_view.dart';
import 'package:mziot/app/modules/devices/add_device/views/add_device_view.dart';
import 'package:mziot/app/modules/devices/views/device_manager_view.dart';
import 'package:mziot/app/modules/devices/wifi/views/wifi_view.dart';
import 'package:mziot/app/modules/home/views/devices_view.dart';
import 'package:mziot/app/modules/home/views/home_view.dart';
import 'package:mziot/app/modules/init/bindings/init_binding.dart';
import 'package:mziot/app/modules/init/views/help_view.dart';
import 'package:mziot/app/modules/init/views/language_view.dart';
import 'package:mziot/app/modules/init/views/launch_view.dart';
import 'package:mziot/app/modules/init/views/start_view.dart';
import 'package:mziot/app/modules/init/views/timezone_view.dart';
import 'package:mziot/app/modules/lighting/views/lighting_view.dart';
import 'package:mziot/app/modules/scene/views/scene_detail.dart';
import 'package:mziot/app/modules/scene/views/scene_view.dart';
import 'package:mziot/app/modules/tabbar/views/tabbar_view.dart';
import 'package:mziot/app/tools/global_const.dart';
import 'package:shared_preferences/shared_preferences.dart';

part 'app_routes.dart';

class AppPages {
  AppPages._();

  static Future<String> INITIAL([bool init = false]) async {
    final shareData = await SharedPreferences.getInstance();
    final isStart = shareData.getBool(Constant.kStart) ?? false;
    final int? language = shareData.getInt(Constant.kLanguage);
    final String user = shareData.getString(Constant.kUser) ?? '';
    final String firstInstall = shareData.getString(Constant.kInstall) ?? '';
    final needZone = GlobalConst.userModel?.isNeedSetTimezone ?? true;

    String? page;
    // if (isStart) {
    //   page = Routes.LAUNCH;
    // } else
    if (language == null) {
      page = Routes.LANGUAGE;
    } else if (user.isNotEmpty && firstInstall.isEmpty) {
      page = Routes.HELP;
    } else if (user.isNotEmpty && needZone) {
      page = Routes.TIME_ZONE;
    } else if (user.isEmpty) {
      page = Routes.AUTH_START;
    } else {
      page = Routes.TABBAR;
    }
    if (!init) {
      Get.offNamedUntil(page, (route) => false);
    }

    return Future.value(page);
  }

  static final routes = [
    GetPage(
      name: _Paths.HOME,
      page: () => HomeView(),
      binding: HomeBinding(),
    ),
    GetPage(
      name: _Paths.AUTH_START,
      page: () => StartView(),
      binding: InitBinding(),
    ),
    GetPage(
      name: _Paths.AUTH_LOGIN,
      page: () => AuthLoginView(),
      binding: LoginBinding(),
    ),
    GetPage(
      name: _Paths.AUTH_REGISTER,
      page: () => AuthRegisterView(),
      binding: RegisterBinding(),
    ),
    GetPage(
      name: _Paths.AUTH_PROFILE,
      page: () => AuthProfileView(),
      binding: ProfileBinding(),
    ),
    GetPage(
      name: _Paths.AUTH_PASSWORD,
      page: () => PasswordView(),
      binding: PassBinding(),
    ),
    GetPage(
      name: _Paths.AUTH_INSWEB,
      page: () => InswebView(),
      binding: WebBinding(),
    ),
    GetPage(
      name: _Paths.TABBAR,
      page: () => TabbarView(),
      binding: TabbarBinding(),
    ),
    GetPage(
      name: _Paths.LANGUAGE,
      page: () => LanguageView(),
      binding: InitBinding(),
    ),
    GetPage(
      name: _Paths.TIME_ZONE,
      page: () => TimezoneView(),
      binding: InitBinding(),
    ),
    GetPage(
      name: _Paths.GROUP_LIST,
      page: () => DevicesView(),
      binding: HomeBinding(),
    ),
    GetPage(
      name: _Paths.LIGHTING,
      page: () => LightingView(),
      binding: LightBinding(),
    ),
    GetPage(
      name: _Paths.DEVICE_MANAGER,
      page: () => DeviceManagerView(),
      binding: DeviceMBinding(),
    ),
    GetPage(
      name: _Paths.ADD_DEVICE,
      page: () => AddDeviceView(),
      binding: DeviceAddBinding(),
    ),
    GetPage(
      name: _Paths.WIFI,
      page: () => WifiView(),
      binding: WifiBinding(),
    ),
    GetPage(
      name: _Paths.SCENE,
      page: () => SceneView(),
      binding: SceneBinding(),
    ),
    GetPage(
      name: _Paths.CIRCLE,
      page: () => CircleView(),
      binding: CircleBinding(),
    ),
    GetPage(
      name: _Paths.SCENE_DETAIL,
      page: () => SceneDetailView(),
      binding: SceneDetailBinding(),
    ),
    GetPage(
      name: _Paths.CIRCLE_DETAIL,
      page: () => CircleDetailView(),
      binding: CircleDetailBinding(),
    ),
    GetPage(
      name: _Paths.HELP,
      page: () => HelpView(),
      binding: InitBinding(),
    ),
    GetPage(
      name: _Paths.LAUNCH,
      page: () => LaunchView(),
      binding: InitBinding(),
    ),
  ];
}
